{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0360be02",
   "metadata": {},
   "source": [
    "# Enum parser\n",
    "\n",
    "This notebook shows how to use an Enum output parser."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2f039b4b",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.output_parsers.enum import EnumOutputParser"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "9a35d1a7",
   "metadata": {},
   "outputs": [],
   "source": [
    "from enum import Enum\n",
    "\n",
    "\n",
    "class Colors(Enum):\n",
    "    RED = \"red\"\n",
    "    GREEN = \"green\"\n",
    "    BLUE = \"blue\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "a90a66f5",
   "metadata": {},
   "outputs": [],
   "source": [
    "parser = EnumOutputParser(enum=Colors)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "c517f447",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_community.chat_models import ChatOpenAI\n",
    "from langchain_core.prompts import PromptTemplate\n",
    "\n",
    "prompt = PromptTemplate.from_template(\n",
    "    \"\"\"What color eyes does this person have?\n",
    "\n",
    "> Person: {person}\n",
    "\n",
    "Instructions: {instructions}\"\"\"\n",
    ").partial(instructions=parser.get_format_instructions())\n",
    "chain = prompt | ChatOpenAI() | parser"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "088f634c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Colors.BLUE: 'blue'>"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chain.invoke({\"person\": \"Frank Sinatra\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8f0a5f80",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
